home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / misc / unix / tracker_4_3.lzh / tracker / randomize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-13  |  2.1 KB  |  94 lines

  1. /* randomize.c 
  2.     vi:se ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: randomize.c,v 4.0 1994/01/11 17:55:13 espie Exp espie $ 
  6.  * $Log: randomize.c,v $
  7.  * Revision 4.0  1994/01/11  17:55:13  espie
  8.  * *** empty log message ***
  9.  *
  10.  * Revision 1.3  1994/01/06  19:22:23  Espie
  11.  * *** empty log message ***
  12.  *
  13.  * Revision 1.1  1993/12/26  00:55:53  Espie
  14.  * Initial revision
  15.  *
  16.  * Revision 3.2  1992/12/03  15:00:50  espie
  17.  * restore stty.
  18.  *
  19.  * Revision 3.1  1992/11/19  20:44:47  espie
  20.  * Protracker commands.
  21.  *
  22.  * Revision 3.0  1992/11/18  16:08:05  espie
  23.  * New release.
  24.  *
  25.  */
  26.  
  27. /* input: a series of names (as argv[1:argc - 1])
  28.  * output: the same names, in a random order.
  29.  * with the new database lookup facility, very useful for e.g.,
  30.  * tracker `randomize *` (jukebox)
  31.  */
  32.  
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include "defs.h"
  36.  
  37. ID("$Id: randomize.c,v 4.0 1994/01/11 17:55:13 espie Exp espie $")
  38.  
  39. /* n = random_range(max): output a number in the range 0:max - 1.
  40.  * For our purpose, we don't have to get a very random number,
  41.  * so the standard generator is alright.
  42.  */
  43. int random_range(max)
  44. int max;
  45.     {
  46.     static init = 0;
  47.  
  48.         /* initialize the generator to an appropriate seed eventually */
  49.     if (!init)
  50.         {
  51.         srand(time(0));
  52.         init = 1;
  53.         }
  54.     return rand()%max;
  55.     }
  56.  
  57. /* output(s): output s in a suitable format. Ideally, output() should use
  58.  * the shell quoting conventions for difficult names. Right now, it doesn't
  59.  */
  60. void output(s)
  61. char *s;
  62.     {
  63.     for(; *s; s++)
  64.         switch(*s)
  65.             {
  66.     /*    case ' ':
  67.         case '(':
  68.         case ')':
  69.         case '\\':
  70.             putchar('\\');
  71.             */
  72.         default:
  73.             putchar(*s);
  74.             }
  75.     putchar(' ');
  76.     }
  77.  
  78. int main(argc, argv)
  79. int argc;
  80. char *argv[];
  81.     {
  82.     int i, k;
  83.  
  84.         /* set up everything so that our names are in argv[0 : argc - 2] */
  85.     for (i = argc - 1, argv++; i; i--)
  86.         {
  87.             /* invariant: the remaining names are in argv[0: i - 1] */
  88.         k = random_range(i);
  89.         output(argv[k]);
  90.         argv[k] = argv[i - 1];
  91.         }
  92.    exit(0);
  93.     }
  94.